package com.persondemo.videoappliction.ui.duanzi.presenter;

import android.text.TextUtils;

import com.persondemo.videoappliction.bean.ZiXunBean;
import com.persondemo.videoappliction.net.BaseApi;
import com.persondemo.videoappliction.net.BaseObserver;
import com.persondemo.videoappliction.net.RxSchedulers;
import com.persondemo.videoappliction.ui.base.BasePresenter;
import com.persondemo.videoappliction.ui.duanzi.contract.ZiXunContract;

import javax.inject.Inject;

public class ZiXunPresenter extends BasePresenter<ZiXunContract.View> implements ZiXunContract.Presenter {
    private BaseApi baseApi;

    @Inject
    public ZiXunPresenter(BaseApi baseApi) {
        this.baseApi = baseApi;
    }

    @Override
    public void getzixun(String page) {
        baseApi.getzixun(page)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(new BaseObserver<ZiXunBean>() {
                    @Override
                    public void onSuccess(ZiXunBean ziXunBean) {
                        ZiXunBean.DataBean dataBean1=null;
                        if (ziXunBean.getData()!=null&&ziXunBean.getStatus()==1){
                            for (int i=0;i<ziXunBean.getData().size();i++){
                                dataBean1=ziXunBean.getData().get(i);
                                if (TextUtils.isEmpty(ziXunBean.getData().get(i).getImgs().toString())){
                                    dataBean1.itemType=ZiXunBean.DataBean.types_01;
                                }else if (ziXunBean.getData().get(i).getImgs().size()==1){
                                    dataBean1.itemType=ZiXunBean.DataBean.types_02;
                                }else {
                                    dataBean1.itemType=ZiXunBean.DataBean.types_03;
                                }
                            }
                        }
                        if (page.equals("1")){
                            mView.loadzixun(ziXunBean);
                        }else {
                            mView.loadmore(ziXunBean);
                        }
                    }

                    @Override
                    public void onFail(Throwable e) {
                        mView.showFaild();
                    }
                });
    }
}
